/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.nxu.campus.zhxy.service.impl;

import com.nxu.campus.utils.FileUtil;
import com.nxu.campus.utils.PageUtil;
import com.nxu.campus.utils.QueryHelp;
import com.nxu.campus.utils.ValidationUtil;
import com.nxu.campus.zhxy.domain.ZhxyCourse;
import com.nxu.campus.zhxy.repository.ZhxyCourseRepository;
import com.nxu.campus.zhxy.service.ZhxyCourseService;
import com.nxu.campus.zhxy.service.dto.ZhxyCourseDto;
import com.nxu.campus.zhxy.service.dto.ZhxyCourseQueryCriteria;
import com.nxu.campus.zhxy.service.mapstruct.ZhxyCourseMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author HWL
* @date 2022-04-15
**/
@Service
@RequiredArgsConstructor
public class ZhxyCourseServiceImpl implements ZhxyCourseService {

    private final ZhxyCourseRepository zhxyCourseRepository;
    private final ZhxyCourseMapper zhxyCourseMapper;

    @Override
    public Map<String,Object> queryAll(ZhxyCourseQueryCriteria criteria, Pageable pageable){
        Page<ZhxyCourse> page = zhxyCourseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zhxyCourseMapper::toDto));
    }

    @Override
    public List<ZhxyCourseDto> queryAll(ZhxyCourseQueryCriteria criteria){
        return zhxyCourseMapper.toDto(zhxyCourseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ZhxyCourseDto findById(Long courseId) {
        ZhxyCourse zhxyCourse = zhxyCourseRepository.findById(courseId).orElseGet(ZhxyCourse::new);
        ValidationUtil.isNull(zhxyCourse.getCourseId(),"ZhxyCourse","courseId",courseId);
        return zhxyCourseMapper.toDto(zhxyCourse);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZhxyCourseDto create(ZhxyCourse resources) {
        return zhxyCourseMapper.toDto(zhxyCourseRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ZhxyCourse resources) {
        ZhxyCourse zhxyCourse = zhxyCourseRepository.findById(resources.getCourseId()).orElseGet(ZhxyCourse::new);
        ValidationUtil.isNull( zhxyCourse.getCourseId(),"ZhxyCourse","id",resources.getCourseId());
        zhxyCourse.copy(resources);
        zhxyCourseRepository.save(zhxyCourse);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long courseId : ids) {
            zhxyCourseRepository.deleteById(courseId);
        }
    }

    @Override
    public void download(List<ZhxyCourseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZhxyCourseDto zhxyCourse : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("课程名称", zhxyCourse.getCourseName());
            map.put("类型", zhxyCourse.getCourseType());
            map.put("学分", zhxyCourse.getCourseCredits());
            map.put("专业", zhxyCourse.getMajor().getName());
            map.put("年级", zhxyCourse.getGrade());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
